<template>
  <el-dialog width="500px" v-bind="$attrs" @close="handleClose" @open="handleOpen">
    <el-form @submit.prevent.native label-position="top" :model="form" ref="form" :rules="rules">
      <el-form-item :label="$t('appstore.application.mrchName') + ':'" prop="merchantId">
        <el-select
          v-model="form.merchantId"
          filterable
          :placeholder="$t('page.tip_select')"
          size="medium"
          style="width: 100%"
        >
          <el-option
            v-for="item in merchantList"
            :key="item.mrchId"
            :label="item.name"
            :value="item.mrchId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('batch.model') + ':'" prop="modelCode">
        <el-select
          v-model="form.modelCode"
          clearable
          :placeholder="$t('batch.model')"
          style="width: 100%"
        >
          <el-option
            v-for="item in modelList"
            :key="item.modelCode"
            :value="item.modelCode"
            :label="item.modelName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="SN" prop="sn">
        <el-input
          type="textarea"
          maxlength="256"
          :rows="5"
          show-word-limit
          v-model="form.sn"
          @blur="() => (form.sn = form.sn.trim())"
          :placeholder="$t('page.sn_list_tip')"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button text @click="handleClose()">{{ $t('common.cancel') }}</el-button>
        <el-button type="primary" :loading="confirmLoading" @click="confirm()">{{
          $t('common.confirm')
        }}</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script>
import { getModelList, addTerminal } from '@/api/tms'
import { getMerchantList } from '@/api/merchant'
export default {
  data() {
    return {
      confirmLoading: false,
      form: {
        merchantId: '',
        modelCode: '',
        sn: ''
      },
      modelList: [],
      merchantList: []
    }
  },
  computed: {
    rules() {
      return {
        merchantId: [
          {
            required: true,
            trigger: 'blur',
            message: this.$t('page.tip_select')
          }
        ],
        modelCode: [
          {
            required: true,
            trigger: 'blur',
            message: this.$t('page.tip_select')
          }
        ],
        sn: [
          {
            required: true,
            trigger: 'blur',
            message: this.$t('page.tip_enter')
          }
        ]
      }
    }
  },
  mounted() {
    this.getModel()
    this.getMerchant()
  },
  methods: {
    getMerchant() {
      let params = {
        status: 1,
        pageNo: 1,
        pageSize: 999
      }
      getMerchantList(params)
        .then((res) => {
          this.merchantList = res.data
        })
        .catch((err) => {
          this.$confirmError(err)
        })
    },
    getModel() {
      getModelList()
        .then((res) => {
          this.modelList = res
        })
        .catch((err) => {
          this.$confirmError(err)
        })
    },
    handleOpen() {},
    handleClose() {
      this.$refs['form'].resetFields()
      this.$emit('update:model-value', false)
    },
    confirm() {
      this.$confirm(this.$t('modules.pass.add_terminal_tip'), this.$t('modules.pass.warning'), {
        confirmButtonText: this.$t('common.confirm'),
        cancelButtonText: this.$t('common.cancel'),
        type: 'info'
      })
        .then(() => {
          this.$refs['form'].validate((valid) => {
            if (valid) {
              this.confirmLoading = true
              let data = {
                merchantId: this.form.merchantId,
                modelCode: this.form.modelCode,
                sns: this.form.sn
              }
              addTerminal(data)
                .then((res) => {
                  this.confirmLoading = false
                  this.$emit('update:model-value', false)
                  this.$emit('refresh')
                  if (res.failedList.length === 0) {
                    this.$message({
                      type: 'success',
                      message: this.$t('page.add_success')
                    })
                  } else {
                    let failMessage = 'SN:'
                    for (let i = 0; i < res.failedList.length; i++) {
                      failMessage += res.failedList[i].sn + ','
                    }
                    failMessage += res.failedList[0].failedReason
                    this.$message({
                      type: 'error',
                      message: failMessage
                    })
                  }
                })
                .catch((err) => {
                  this.confirmLoading = false
                  this.$confirmError(err)
                })
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: this.$t('modules.pass.add_canceled')
          })
        })
    }
  }
}
</script>

<style></style>
